﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AGShell
{
    public class AGGDI
    {
        private Graphics _primaryGraphics;
        private Graphics _graphics;
        private Image _image;

        public void Init(System.Windows.Forms.Form form)
        {
            _primaryGraphics = Graphics.FromHwnd(form.Handle);

            _image = new Bitmap(MainWindow.Width, MainWindow.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

            _graphics = Graphics.FromImage(_image);
        }

        public void DrawText(string text, float x, float y)
        {
            _graphics.DrawString(text, AGRES.DEBUGFONT, Brushes.White, x, y);
        }

        public void DrawShadowText(string text, float x, float y)
        {
            _graphics.DrawString(text, AGRES.DEBUGFONT, Brushes.Gray, x + 1, y);
            _graphics.DrawString(text, AGRES.DEBUGFONT, Brushes.Gray, x, y + 1);
            _graphics.DrawString(text, AGRES.DEBUGFONT, Brushes.Gray, x + 1, y + 1);
            _graphics.DrawString(text, AGRES.DEBUGFONT, Brushes.Red, x, y);
        }

        public void DrawRectangle(float x, float y, float w, float h)
        {
            _graphics.DrawRectangle(Pens.Green, x, y, w, h);
        }

        public void DrawBlock(float x, float y, float w, float h)
        {
            _graphics.DrawRectangle(Pens.Red, x, y, w, h);
            _graphics.DrawLine(Pens.Red, x, y, x + w, y + h);
            _graphics.DrawLine(Pens.Red, x, y + h, x + w, y);
        }

        public void DrawImage(Bitmap image, float x, float y, float width, float height, float originalWidth, float originalHeight)
        {
            ImageAttributes attr = new ImageAttributes();
            attr.SetColorKey(Color.Black, Color.Black);
            _graphics.DrawImage(image, new Rectangle((int)x, (int)y, (int)width, (int)height), 0, 0, originalWidth, originalHeight, GraphicsUnit.Pixel, attr);
        }

        public void DrawImage(Bitmap image, float x, float y, float width, float height, float srcX, float srcY, float originalWidth, float originalHeight)
        {
            ImageAttributes attr = new ImageAttributes();
            attr.SetColorKey(Color.Black, Color.Black);
            _graphics.DrawImage(image, new Rectangle((int)x, (int)y, (int)width, (int)height),srcX, srcY, originalWidth, originalHeight, GraphicsUnit.Pixel, attr);
        }

        public void Clear()
        {
            _graphics.Clear(Color.Black);
        }

        public void Flush()
        {
            _primaryGraphics.DrawImage(_image, 0, 0);
        }
    }
}
